#include "stdafx.h"
#include "HTTPConfig.h"

//初始化
HTTPConfig *HTTPConfig::m_instance = new HTTPConfig();

HTTPConfig::~HTTPConfig()
{
	delete this->xmlDoc;

	this->xmlDoc = NULL;
}

/*获取端口*/
unsigned int HTTPConfig::getPort()
{
	std::string value =  this->get("/settings/general/serverPort");

	if (value.size() == 0) {
		return 8080;
	}


	return std::stoi(value);
}

std::string HTTPConfig::getIP()
{
	return std::string();
}

HTTPConfig * HTTPConfig::getInstance(const std::string & str)
{
	m_instance->setConfigFile(str);
		
	return m_instance;
}


void HTTPConfig::setConfigFile(const std::string & str)
{
	this->configFile = str;
}

bool HTTPConfig::isDocVisible()
{
	std::string value = this->get("/settings/general/directoryVisible");

	if (value.size() == 0)
	{
		return false;		
	}

	return value == "yes";
	
}

std::string HTTPConfig::getWebPath()
{
	std::string value = this->get("/settings/general/docRoot");

	if (value.size() == 0)
	{
		return "www";
	}

	return value;
}

std::string HTTPConfig::getTmpRoot()
{
	std::string value = this->get("/settings/general/maxConnections");

	if (value.size() == 0)
	{
		return "temp";
	}

	return value;
}

unsigned int HTTPConfig::getMaxConnect()
{
	std::string value = this->get("/settings/general/maxConnections");

	if (value.size() == 0)
	{
		return 1000;
	}
	return std::stoi(value);
}

unsigned int HTTPConfig::getMaxConnectionsPerIpNumber()
{
	std::string value = get("/settings/general/maxConnectionsPerIP");

	if (value.size() == 0)
	{
		/*
		* 默认值: 0, 表示不限制
		*/
		return 0;
	}
	return std::stoi(value);
}

bool HTTPConfig::load()
{
	if (this->xmlDoc->Load(this->configFile.c_str(), true))
	{
		return true;
	}
	else
	{
		return this->xmlDoc->Build("settings", "1.0", "utf-8") != NULL;
	}
}

unsigned int HTTPConfig::getTimeout()
{
	std::string value = this->get("/settings/general/maxConnections");

	if (value.size() == 0)
	{
		return 1000;
	}
	return std::stoi(value);
}

/*销毁实例*/
void HTTPConfig::DestroyInstance()
{
	if (m_instance)
		delete m_instance;
	m_instance = NULL;
}

HTTPConfig::HTTPConfig(std::string & fileName)
{
	this->xmlDoc = new XMLDocument();

	this->configFile = fileName;
}

HTTPConfig::HTTPConfig()
{
	this->xmlDoc = new XMLDocument();
}

std::string HTTPConfig::get(const std::string & itemName)
{
	this->xmlHandle = this->xmlDoc->GetNode(itemName.c_str());

	if (this->xmlHandle)
	{
		return this->xmlDoc->GetContent(this->xmlHandle);
	}

	return "";
}

unsigned long HTTPConfig::getMaxConnectionSpeed()
{
	std::string value = get("/settings/general/maxSpeed");
	
	if (value.size() == 0)
	{
		/*
		* 默认值: 0, 表示不限制
		*/
		return 0;
	}
	return std::stoul(value);
	
}

unsigned long HTTPConfig::getSessionTimeout()
{

	std::string value = get("/settings/general/sessionTimeout");

	if (value.size() == 0)
	{
		/*
		* 默认值: 0, 表示不限制
		*/
		return 0;
	}
	return std::stoul(value);
}

unsigned long HTTPConfig::getRecvTimeout()
{

	std::string value = get("/settings/general/recvTimeout");

	if (value.size() == 0)
	{
		/*
		* 默认值: 0, 表示不限制
		*/
		return 5000;
	}
	return std::stoul(value);
}

unsigned long HTTPConfig::getSendTimeout()
{

	std::string value = get("/settings/general/sendTimeout");

	if (value.size() == 0)
	{
		/*
		* 默认值: 0, 表示不限制
		*/
		return 5000;
	}
	return std::stoul(value);
}

unsigned long HTTPConfig::getKeepAliveTimeout()
{

	std::string value = get("/settings/general/keepAliveTimeout");

	if (value.size() == 0)
	{
		/*
		* 默认值: 0, 表示不限制
		*/
		return 0;
	}
	return std::stoul(value);
}
